package lvcy.message.sender.builder.impl;

import java.lang.reflect.Constructor;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import lvcy.message.entity.RequestEntity;
import lvcy.message.sender.HytMessageSender;
import lvcy.message.sender.MessageSender;
import lvcy.message.sender.builder.HytMessageSenderBuilder;

public class HytPackageMessageSenderBuilder extends HytMessageSenderBuilder{


	private Logger log=LogManager.getLogger(this);
	@Override
	public MessageSender build(RequestEntity request) {
		
		try {
			Class<?> clazz=Class.forName(HytMessageSender.class.getName());
			Constructor<?> constructor=clazz.getDeclaredConstructor(String.class,String.class);
			this.sender=(HytMessageSender)constructor.newInstance("",getPostData(request));
		} catch (Exception e) {
			log.error(e);
		}
		return sender;
	}
	@Override
	protected String getPostData(RequestEntity request){
		
		StringBuilder builder=new StringBuilder(super.getPostData(request));
		
		@SuppressWarnings("unchecked")
		Map<String, String> msgs=request.getExtras().get("msgs")==null?null:(Map<String, String>)request.getExtras().get("msg");
		if(msgs==null){
			throw new IllegalArgumentException("please put the msg map");
		}
		for(Map.Entry<String, String> entry:msgs.entrySet()){
			builder.append("msg=").append(entry.getKey()).append("|").append(entry.getValue()).append("&");
		}
		return builder.toString();
	}

}
